﻿@page "/AddLeaveData"
@using EmployeeLeaveSystem.Shared.Models;
@implements IPageFlush;
@using System.ComponentModel.DataAnnotations;
@using System.ComponentModel.DataAnnotations.Schema;
@using EmployeeLeaveSystem.Shared.DAO;
@inject HttpClient _httpClient;
@inject IMessageService _message
@inject NavigationManager _NavigationManager

<PageTitle>新增请假</PageTitle>
@if (GobalManager.myInfo == null) {
    <div>
        <Alert ShowIcon="true" Message="您尚未登录，点击右上角的“登录”来登录。" Type="@AlertType.Error" />
    </div>
} else {
    <div>
        <Title Level="1">请假申请表</Title>
        <Paragraph>欢迎使用中北请假王，本系统用于请假，填写信息务必真实有效，否则将可能被退回。</Paragraph>
        <Card Title="我的个人信息" Style="width:500px;">
            <Extra>
                <Button>更多</Button>
            </Extra>
            <Body>
                <GridRow>
                    <GridCol Span="6" Style="width:100px;">
                        <div style="background-image:url('@($"/ImgsReq/{GobalManager.myInfo?.Avatar_Name}")');width:100px;height:100px;background-size:cover;"></div>
                    </GridCol>
                    <GridCol>
                        <GridRow Style="width:300px">
                            <GridCol Span="12">
                                <div><Text Strong>姓名</Text></div>
                            <div>@GobalManager.myInfo?.Name</div>
                            <div><Text Strong>描述</Text></div>
                            <div>@GobalManager.myInfo?.Description</div>
                        </GridCol>
                        <GridCol>
                            <div><Text Strong>所属职工部门</Text></div>
                            <Tag Color="orange-inverse">@GobalManager.myInfo?.Department?.Name</Tag>
                            <div><Text Strong>联系方式</Text></div>
                            <div><Tag Icon="youtube" Color="#cd201f">Youtube</Tag></div>
                        </GridCol>
                    </GridRow>

                </GridCol>
            </GridRow>
        </Body>
    </Card>
    <Paragraph />
    <Paragraph>
        <Form Model="@leaveDetails"
              Layout="@FormLayout.Vertical"
              OnFinish="SubmitInfos">

            <FormItem Label="">
                <Title Level="2">标题</Title>
                <Paragraph>填写请假单的标题，务必简明扼要。</Paragraph>
                <Input Disabled="isUpdate | isSuccessAdd" @bind-Value="@context.Name" />
            </FormItem>
            <FormItem Label="">
                <Title Level="2">描述</Title>
                <Paragraph>填写请假单的详细信息，用于理由描述。填写请假的事项以及理由等原因。</Paragraph>
                <TextArea Disabled="isUpdate | isSuccessAdd" @bind-Value="@context.Description" />
            </FormItem>

            <FormItem Label="">
                <Title Level="2">起始日期 至 结束日期</Title>
                <Paragraph>选择起始的日期 到 结束的日期</Paragraph>
                <RangePicker Disabled="isUpdate | isSuccessAdd" ShowTime="true" AllowClear="false" @bind-Value="@context.rangePicker" />
            </FormItem>

            <FormItem Label="">
                <Title Level="2">请假类型</Title>
                <Paragraph>从下拉菜单中选择一个合适的类型。</Paragraph>
                @{
                        switch (leaveDetails.Type) {
                            case LeaveType.CasualLeave: {
                                    <div><Text Code>事假</Text>当有事情的时候，使用这条。</div>
                                    break;
                                }
                            case LeaveType.SickLeave: {
                                    <div><Text Code>病假</Text>当您需要去医院的时候，使用这条理由。</div>
                                    break;
                                }
                            case LeaveType.Business: {
                                    <div><Text Code>出差</Text>选择这条理由会非常好，因为公司鼓励你出差。</div>
                                    break;
                                }
                            case LeaveType.PaidLeave: {
                                    <div><Text Code>带薪休假</Text>建议你不要选择这条原因，因为你不太可能成功请到假，公司是996狼性文化公司。</div>
                                    break;
                                }
                            default: {
                                    break;
                                }
                        }
                    }
                    <Paragraph />
                    <div>
                        <RadioGroup Disabled="isUpdate | isSuccessAdd" @bind-Value="@context.Type" ButtonStyle="RadioButtonStyle.Solid">
                            <Radio RadioButton="true" Value="@LeaveType.CasualLeave">事假</Radio>
                            <Radio RadioButton="true" Value="@LeaveType.SickLeave">病假</Radio>
                            <Radio RadioButton="true" Value="@LeaveType.Business">出差</Radio>
                            <Radio RadioButton="true" Value="@LeaveType.PaidLeave">带薪休假</Radio>
                        </RadioGroup>
                    </div>
                </FormItem>

                <FormItem Label="">
                    <Title Level="2">审核人</Title>
                    <Paragraph>请选择一个审核人进行审核</Paragraph>
                    <Select Mode="default"
                        DataSource="@staffs"
                        LabelName="@nameof(StaffInfo.Name)"
                        Disabled="isUpdate | isSuccessAdd"
                        @bind-Value=@context.AcceptStaff>
                    </Select>
                    <Paragraph></Paragraph>
                    <Paragraph>
                        <div>
                            审核人信息： <Tag Color="orange-inverse">@leaveDetails.AcceptStaff?.Department.Name</Tag>
                            @if (context.AcceptStaff?.Id == GobalManager.myInfo.UpperStaffId) {
                                <Tag>上司</Tag>
                            }
                        </div>
                    </Paragraph>
                    <Paragraph>
                        <Button>查看审核人详细信息</Button>
                    </Paragraph>
                </FormItem>

                <Divider />

                <Paragraph>
                    <Text>确认好信息后，即可进行提交。请一定确保信息真实有效。</Text>
                </Paragraph>
                <Paragraph>
                    <Text>递交日期 @DateTime.Now.ToString("yyyy-MM-dd")</Text>
                </Paragraph>

                <FormItem>
                    @if (isSuccessAdd == true) {
                        <div style="color:red">您已经成功递交过本次表格，请勿重复递交</div>
                    }
                    <Button Type="@ButtonType.Primary" HtmlType="submit" Block Loading="@isUpdate" Disabled="isSuccessAdd">
                    递交
                    </Button>
                    <Divider></Divider>
                    <Paragraph>如果想要清除上方的信息，点击下面的按钮来清除。</Paragraph>
                    <Button Danger HtmlType="reset" Disabled="isSuccessAdd">
                    清除
                    </Button>
                    </FormItem>

                    </Form>
                    </Paragraph>
                </div>
}


@code {
    class LeaveDatas {
        [MinLength(3)]
        [MaxLength(50)]
        [Required]
        public string Name { get; set; } = "【新建请假单】";
        public DateTime LeaveDate { get; set; } = DateTime.Now; //请假发起日期
        [Required]
        public StaffInfo? AcceptStaff { get; set; } // 审核确认人
        [Required]
        public LeaveType Type { get; set; } = LeaveType.CasualLeave;
        public DateTime[] rangePicker { get; set; } = new DateTime[] { DateTime.Now, DateTime.Now.AddDays(5) }; // 请假时间选择
        public String Description { get; set; } = "";//请假原因描述
        public AcceptFlag AcceptFlag { get; set; } = AcceptFlag.审核中;//请假确认标志
    }
    private int currentCount = 0;
    private static LeaveDatas leaveDetails = new();
    private static List<StaffInfo> staffs = new() { };
    private bool isUpdate = false;
    private static bool isSuccessAdd = false;


    protected override void OnInitialized() {
        // 通知GobManager去获取相关的人员信息供填写使用
        GobalManager.AddPage(this);
        base.OnInitialized();
    }

    protected override Task OnInitializedAsync() {
        this.GetManagerInfos();
        return base.OnInitializedAsync();
    }

    private void IncrementCount() {
        currentCount++;
    }

    private async void GetManagerInfos() {
        await App._appHandle.GetAllStaffInfos();
        staffs.Clear();
        for (int i = 0; i < GobalManager.staffInfos.Count; i++) {
            var infos = GobalManager.staffInfos[i];
            if (GobalManager.staffInfos[i].Type == StaffType.Manager) {
                staffs.Add(infos);
            }
        }
        InvokeAsync(StateHasChanged);
    }

    private async void SubmitInfos() { // 开始递交请假表信息
        _message.Info("正在递交中");
        if (isUpdate) return;
        isUpdate = true;
        var data = new LeaveDetail();
        data.StaffId = GobalManager.myInfo?.Id;
        data.Name = leaveDetails.Name;
        data.Description = leaveDetails.Description;
        data.Type = leaveDetails.Type;
        data.StartTime = leaveDetails.rangePicker[0];
        data.EndTime = leaveDetails.rangePicker[1];
        data.AcceptStaffId = leaveDetails.AcceptStaff.Id;
        await Task.Delay(1000);
        try {
            var res = await _httpClient.PostAsJsonAsync<LeaveDetail>(@"Main/AddLeaveDetail", data);
            isUpdate = false;
            ResultAdd._NetData = await res.Content.ReadFromJsonAsync<NetworkData>();
            _NavigationManager.NavigateTo("AddLeaveData/ResultAdd");

            //isSuccessAdd = true;
        }
        catch (Exception ex) {
            isUpdate = false;
            App._appHandle.NoticeWithIcon(NotificationType.Success, "网络状态", "递交失败，原因：" + ex.Message);
        }
    }

    public void Flush() {
        StateHasChanged();
    }
}
